package restless.core.managers {
	
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import restless.core.interfaces.I2d;
	import restless.core.gameObjs.RLEManagedObj;
	import restless.core.gameObjs.RLETextObj;
	import restless.utilities.RLEStringUtils;
	
	/**
	 * @author MNS
	 */
	 
	public class RLETextManager extends RLEManagedObj {
		
		protected var format:TextFormat;
		protected var textObj:RLETextObj;
		protected var vFormats:Vector.<TextFormat> = new Vector.<TextFormat>;
		protected var vFontIDs:Vector.<String> = new Vector.<String>;
		protected var vFormatIDs:Vector.<String> = new Vector.<String>;
		protected var fonts:Array;
		protected var aRLEFonts:Array = [];
		
		public var DEFAULT_FONT:String = "DefaultFont";
		public var DEFAULT_FORMAT:String = "DefaultFormat";
		
		public function RLETextManager() {
			setRE();
			fonts = Font.enumerateFonts();
			fonts.sortOn("fontName", Array.CASEINSENSITIVE);
			
			for (var i:int = 0; i < fonts.length; i++) {
				vFontIDs.push(fonts[i].fontName);
				aRLEFonts.push(fonts[i]);
				trace("Loded Fonts = ", fonts[i].fontName + ", " + fonts[i].fontStyle);
			}
		}
		
		public function addFont(font:Class, id:String):void {
			var f:* = new font;
			vFontIDs.push(id);
			aRLEFonts.push(f);
		}
		
		public function addTextFormat(id:String, font:String = null, color:uint = 0x00000000, bold:Boolean = false, size:Number = 32, align:String = null, italic:Boolean = false, underLine:Boolean = false ):void {
			var num:int = vFontIDs.indexOf(font);
			
			tempString = aRLEFonts[num].fontName;
			format = new TextFormat(tempString, size, color, bold, italic, underLine);
			
			if (font == null) {
				format.font = aRLEFonts[0].fontName;;
			}
			
			if (align != null) {
				format.align = align;
			}
			
			vFormatIDs.push(id);
			vFormats.push(format);
			format = null;
		}
		
		public function getTextFormat(id:String):TextFormat {
			var num:int = vFormatIDs.indexOf(id);
			if (num > -1) { 
				return vFormats[num];
			}
			return null;
		}
		
		public function removeTextFormat(id:* ):void {
			var n:int;
			
			if (id == String) {
				n = vFormatIDs.indexOf(id);
			} else {
				n = vFormats.indexOf(id);
			}
			
			vFormatIDs.splice(n, 1);
			vFormats.splice(n, 1);
		}
	}
}